﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Xml.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

using TQB.Web.UI.Comments;

public partial class Comments_comment1_api : System.Web.UI.Page
{
    string action = string.Empty;
    string id = string.Empty;
    string page = string.Empty;
    string pagehtml = string.Empty;
    string linkhtml = string.Empty;
    //int Pages = 5;

    string commentparentid = string.Empty;
    string commentuser = string.Empty;
    string commenttext = string.Empty;
    string commentvalidate = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        action = Request.Params["action"];
        id = Request.Params["id"];
        page = Request.Params["page"];

        commentparentid = Request.Params["commentparentid"];
        commentuser = Request.Params["commentuser"];
        commenttext = Request.Params["commenttext"];
        commentvalidate = Request.Params["commentvalidate"];

        if (action == "ajax_getcomment")
        {
            //ajax_getcomment(id, Int32.Parse(page));
        }
        else if (action == "ajax_sendcomment")
        {

            if (Session["VerifyCode"].ToString().ToLower() != commentvalidate.ToLower())
            {
                Response.Write("验证码错误!");
            }
            else
            {
                DSTestDBTableAdapters.CommentTableAdapter dal = new DSTestDBTableAdapters.CommentTableAdapter();
                dal.Insert("1",
                    Convert.ToInt32(commentparentid),
                    commentuser,
                    Server.HtmlEncode(commenttext),
                    string.Empty,
                    DateTime.Now,
                    Request.ServerVariables["REMOTE_ADDR"].ToString());

                Response.Write("评论发表成功!");
            }
        }
        else
        {
            //Response.Write("error!");
        }
    }

    [WebMethod]
    public static int GetCommnetCount(string id)
    {
        DSTestDBTableAdapters.CommentTableAdapter dal = new DSTestDBTableAdapters.CommentTableAdapter();
        Nullable<int> total = dal.GetCountByparentid(int.Parse(id));
        return total.HasValue ? total.Value : 0;

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id">评论关联ID。</param>
    /// <param name="page">第几页。</param>
    /// <param name="pagesize">分页数</param>
    /// <returns></returns>
    [WebMethod]
    public static IEnumerable GetCommentList(string id, int page, int pagesize)
    {
        DSTestDBTableAdapters.CommentTableAdapter dal = new DSTestDBTableAdapters.CommentTableAdapter();

        DSTestDB.CommentDataTable dt = dal.GetListByParentid(int.Parse(id));

        var results = from dr in dt.AsEnumerable()
                      select new
                      {
                          commentid = dr["commentid"],
                          commentuser = dr["commentuser"],
                          commentdate = DateTime.Parse(dr["commentdate"].ToString())
                                          .ToShortDateString(),
                          commenttext = dr["commenttext"],
                          commentreply = dr["commentreply"]
                      };

        return results.Skip((page - 1) * pagesize).Take(pagesize); ;

    }

    [WebMethod]
    public static string commentreply(int id, string commentreply)
    {
        DSTestDBTableAdapters.CommentTableAdapter dal = new DSTestDBTableAdapters.CommentTableAdapter();
        try
        {
            dal.UpdateReplay(commentreply, id);
            return "ok";
        }
        catch
        {
            return "error";
        }
    }

}